Friend Class TH_Upload_EEPROM
    Inherits Threads.BikiThread

    Private m_data() As Byte
    Private m_Address As UInt16 = 0

    Sub New(ByVal Data As Byte(), ByVal Address As UInt16)
        m_data = Data
        m_Address = Address
    End Sub

    Friend Overloads Function Execute( _
             ByVal backGroundWorker As System.ComponentModel.BackgroundWorker, _
             ByVal e As System.ComponentModel.DoWorkEventArgs) As Boolean
        ' -------------------- Calls the inherited execute routine
        MyBase.Execute(backGroundWorker, e)

        OnMessage("Uploading Bikirom Settings...")
        Dim f_ret As BK_return = MyClass.UploadEEPROM
        If f_ret = BK_return.BK_OK Then
            OnMessage("Uploading Bikirom Settings OK!")
            Dim f_crc As New libCRC32
            SetUsbVariable(Biki_USBvars.UV_BikEEPROM_CRC, f_crc.GetByteArrayCrc32(m_data))
        Else
            'OnMessage(Err_Decode(f_ret))
        End If
    End Function

    Sub New(ByRef data As Byte())
        m_data = ObjectCopy(data)
    End Sub

    Private Function UploadEEPROM() As BK_return
        Dim f_Frame As Comm.Comm_Frame
        Dim i As ULong
        m_BikiInfo.MaxTxPacket = 186
        Dim f_PacketSize As UInt16 = m_BikiInfo.MaxTxPacket
        Dim f_Packet(f_PacketSize - 1) As Byte
        Dim f_return As BK_return = BikiRom_Init()

        If f_return = BK_return.BK_OK Then
            m_BikiInfo.TimeOut = 2000
            Try
                For i = m_Address To UBound(m_data) + m_Address - f_PacketSize Step f_PacketSize
                    Array.Copy(m_data, CLng(i - m_Address), f_Packet, 0, f_PacketSize)

                    f_Frame = New Comm.Comm_Frame(Comm.Comm_Frame.BikiFrameType.ID_EEPROM_DATA, i, Comm.Comm_Frame.BikiFrameAction.WRITE, f_Packet)

                    f_Frame.Send(, , 10)

                    OnProgress(((i - m_Address) / (UBound(m_data))) * 100)
                Next

                If ((UBound(m_data) - m_Address) Mod f_PacketSize) <> 0 Then
                    ' get the final and last packet here... fool :)
                    f_PacketSize = ((UBound(m_data) - m_Address) Mod f_PacketSize) + 1
                    ReDim f_Packet(f_PacketSize - 1)

                    Array.Copy(m_data, CLng(UBound(m_data) - f_PacketSize + 1), f_Packet, 0, f_PacketSize)

                    f_Frame = New Comm.Comm_Frame(Comm.Comm_Frame.BikiFrameType.ID_EEPROM_DATA, (UBound(m_data) + m_Address) - f_PacketSize + 1, Comm.Comm_Frame.BikiFrameAction.WRITE, f_Packet)

                    f_Frame.Send(, , 10)
                End If
                OnProgress(100)
            Catch exComm As Comm_Exceptions
                OnMessage(exComm.Description)
                m_BikiInfo.reset()
                Return BK_return.BK_IO_ERROR_BIK
            Catch exUsb As USB_exceptions
                OnMessage(exUsb.Description)
                m_BikiInfo.reset()
                Return BK_return.BK_IO_ERROR_USB
            End Try
            m_BikiInfo.reset()
        Else
            Return f_return
        End If
    End Function
End Class
